<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Sys\User;
use App\Models\Sys\UserRole;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class UserController extends Controller
{

    public function list(Request $request)
    {
        try {
            $param = $request->all();
            $User = new User();
            $data = $User->getAllUser($param);
            return success($data);
        } catch (Exception $e) {
            return error(null, $e->getMessage());
        }
    }
    public function add(Request $request)
    {
        try {
            $data = $request->all();
            $validator = Validator::make(
                $data,
                [
                    'username' => 'required|unique:sys_user,username',
                    'password' => 'required'
                ],
                [
                    'username.required' => '用户名不能为空',
                    'username.unique' => '用户名必须唯一',
                    'password.required' => '密码不能为空',
                ]
            );
            if ($validator->fails()) {
                throw new Exception($validator->errors()->first());
            }
            $User = new User();
            $User->username = $data['username'];
            $User->password = encrypt($data['password']);
            $User->sex = $data['sex'] ?? null;
            $res = $User->save();
            if (!$res) {
                throw new Exception('添加失败');
            }
            return success();
        } catch (Exception $e) {
            return error(null, $e->getMessage());
        }
    }

    public function delete(Request $request)
    {
        try {
            $data = $request->all();
            $validator = Validator::make(
                $data,
                [
                    'id' => 'required|exists:sys_user,id',
                ],
                [
                    'id.required' => 'id不能为空',
                    'id.exists' => '删除的用户不存在',
                ]
            );
            if ($validator->fails()) {
                throw new Exception($validator->errors()->first());
            }
            $User = new User();
            $data = $User->where('id', '=', $request->id)->delete();
            return success($data);
        } catch (Exception $e) {
            return error(null, $e->getMessage());
        }
    }


    public function edit(Request $request)
    {
        try {
            $data = $request->all();
            $validator = Validator::make(
                $data,
                [
                    'id' => 'required|exists:sys_user,id',
                ],
                [
                    'id.required' => 'id不能为空',
                    'id.exists' => '数据不存在',
                ]
            );
            if ($validator->fails()) {
                throw new Exception($validator->errors()->first());
            }
            $User = new User();
            $result=$User->getUser($data['id']);
            $result['password']=decrypt($result['password']);
            return success($result);
        } catch (Exception $e) {
            return error(null, $e->getMessage());
        }
    }


    public function update(Request $request)
    {
        try {
            $data = $request->all();
            $validator = Validator::make(
                $data,
                [
                    'id' => 'required|exists:sys_user,id',
                    'username' => 'required',
                    'password' => 'required'
                ],
                [
                    'id.required' => 'id不能为空',
                    'id.exists' => '数据不存在',
                    'username.required' => '用户名不能为空',
                    'username.unique' => '用户名必须唯一',
                    'password.required' => '密码不能为空',
                ]
            );
            if ($validator->fails()) {
                throw new Exception($validator->errors()->first());
            }
            $data['password']=encrypt($data['password']);
            $User = new User();
            $res = $User->updateds($data['id'],$data);
            if (!$res) {
                throw new Exception('更新失败');
            }
            return success();
        } catch (Exception $e) {
            return error(null, $e->getMessage());
        }
    }






    // 开始
    public function userRoleList(Request $request)
    {
        try {
            $data = $request->all();
            $validator = Validator::make(
                $data,
                [
                    'id' => 'required',
                ],
                [
                    'id.required' => 'id不能为空',
                ]
            );
            if ($validator->fails()) {
                throw new Exception($validator->errors()->first());
            }
            $UserRole = new UserRole();
            $data = $UserRole->getRoleByUserId($data['id']);
            return success($data);
        } catch (Exception $e) {
            return error(null, $e->getMessage());
        }

    }

    public function userRoleUpdate(Request $request)
    {
        try {
            $data = $request->all();
            $validator = Validator::make(
                $data,
                [
                    'id' => 'required',
                    'roleId'=>'required|array'
                ],
                [
                    'id.required' => 'id不能为空',
                    'roleId.required' => 'roleId不能为空',
                    'roleId.array' => 'roleId必须是数组',
                ]
            );
            if ($validator->fails()) {
                throw new Exception($validator->errors()->first());
            }
            $res=(new UserRole())->saveUserRole($data['id'],$data['roleId']);
            if(!$res){
                throw new Exception('更新失败');
            }
            return success();
            
        } catch (Exception $e) {
            return error(null, $e->getMessage());
        }

    }
}
